package internationalenergytradingplatform.controller.BuyerController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import internationalenergytradingplatform.VO.BuyerCommodityVO;
import internationalenergytradingplatform.VO.TabbarVO;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.CommodityDao;
import internationalenergytradingplatform.dao.CommodityMapper;
import internationalenergytradingplatform.dto.CommodityPageQueryDTO;
import internationalenergytradingplatform.pojo.Classification;
import internationalenergytradingplatform.pojo.Commodity;
import internationalenergytradingplatform.result.PageResult;
import internationalenergytradingplatform.result.Result;
import internationalenergytradingplatform.service.SupplierCommodityService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 名称BuyerCommodityController
 * <p>
 * 描述
 *
 * @version 1.0
 * @author:Zuowenbo
 * @datatime:2024-03-24 16:05
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/buyer-commodity")
public class BuyerCommodityController {

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    SupplierCommodityService supplierCommodityService;

    /**
     * 根据id查找商品信息
     */
    @GetMapping("/{id}")
    public Result<BuyerCommodityVO> getById(@PathVariable Integer id) {
        BuyerCommodityVO buyerCommodityVO = commodityMapper.getById(id);
        log.info("根据id查找商品:{}", buyerCommodityVO);
        return Result.success(buyerCommodityVO);
    }

    /**
     * 分页查询所有商品
     */
    @GetMapping("/paginationQuery")
    public Result<PageResult> pagingToQueryAllCommodity(CommodityPageQueryDTO commodityPageQueryDTO){
        log.info("商品分页查询，参数为：{}", commodityPageQueryDTO);
        PageHelper.startPage(commodityPageQueryDTO.getPage(), commodityPageQueryDTO.getPageSize());
        Page<Commodity> page = commodityMapper.CommodityPageQueryByBuyer(commodityPageQueryDTO);
        long total = page.getTotal();
        List<Commodity> records = page.getResult();
        PageResult pageResult = new PageResult(total, records);
        return Result.success(pageResult);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/29 13:55
     查询某店铺的所有分类
     */
    @GetMapping("/getAllCommodityByShopId/{shopId}")
    public Result<List<Classification>> getAllCommodityByShopId(@PathVariable String shopId){
        List<Classification> allCommodityByShopId = commodityMapper.getAllCommodityByShopId(shopId);
        return Result.success(allCommodityByShopId);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/29 14:00
     查询某分类的所有商品
     */
    @GetMapping("/getAllCommodityByClassificationId/{classificationId}")
    public Result<List<Commodity>> getAllCommodityByClassificationId(@PathVariable Integer classificationId){
        List<Commodity> allCommodityByClassification = commodityMapper.getAllCommodityByClassification(classificationId);
        return Result.success(allCommodityByClassification);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/30 1:01
     查询某店铺所有类别的所有商品
     */
    @GetMapping("getTabbar/{shopId}")
    public Result<List<TabbarVO>> getTabbar(@PathVariable String shopId){
        //创建List对象
        List<TabbarVO> tabbarVOList = new ArrayList<>();
        //获取所有的类别
        List<Classification> allCommodityByShopId = commodityMapper.getAllCommodityByShopId(shopId);
        for(Classification classification : allCommodityByShopId){
            //设置TabbarVO的name
            TabbarVO tabbarVO = new TabbarVO();
            tabbarVO.setName(classification.getName());
            //设置TabbarVO的commodity
            List<Commodity> allCommodityByClassification = commodityMapper.getAllCommodityByClassification(classification.getId());
            tabbarVO.setCommodity(allCommodityByClassification);
            tabbarVOList.add(tabbarVO);
        }
        log.info("tabbarVOList==>{}", tabbarVOList);
        return Result.success(tabbarVOList);
    }

}
